package pers.jing.android.helper.widget

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.FactCheck
import androidx.compose.material.icons.rounded.Check
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.Card
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.VerticalDivider
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.max
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties

/**
 * 我知道了-弹窗
 */
@Composable
fun ConfirmDialog(msg: String, onConfirm: () -> Unit, onDismiss: () -> Unit) {
    Dialog(
        onDismissRequest = {
            onDismiss()
        }
    ) {
        ElevatedCard(
            modifier = Modifier.width(300.dp)
        ) {
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier.fillMaxWidth()
            ) {
                Text(
                    text = "温馨提示",
                    fontSize = 17.sp,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier.padding(top = 10.dp)
                )
                Text(
                    text = msg,
                    fontSize = 14.sp,
                    modifier = Modifier.fillMaxWidth()
                        .padding(horizontal = 10.dp)
                        .padding(top = 10.dp)
                        .heightIn(max = 200.dp)
                        .verticalScroll(rememberScrollState())
                )
                HorizontalDivider(
                    modifier = Modifier.fillMaxWidth().height(1.dp)
                )
                TextButton (
                    modifier = Modifier.fillMaxWidth()
                        .padding(top = 10.dp),
                    shape = RectangleShape,
                    onClick = {
                    onConfirm()
                }) {
                    Text("我知道了")
                }

            }
        }
    }
}


@Composable
fun ProgressDialog(text: String) {
    Dialog(
        onDismissRequest = {

        },
        properties = DialogProperties(
            dismissOnBackPress = false,
            dismissOnClickOutside = false
        )
    ) {
        Card(
            modifier = Modifier.size(120.dp)
        ) {
            Column(
                modifier = Modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                CircularProgressIndicator()
                Text(
                    text = text
                )
            }
        }
    }
}

/**
 * 确认/取消的对话框
 */
@Composable
fun ConfirmCancelDialog(msg: String, onConfirm: () -> Unit,
                        onDismiss: () -> Unit) {
    Dialog(
        onDismissRequest = {
            onDismiss()
        }
    ) {
        Card {
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {
                Text(
                    text = "温馨提示",
                    fontSize = 17.sp,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier.padding(top = 10.dp)
                )
                Text(
                    text = msg,
                    textAlign = TextAlign.Center,
                    fontSize = 14.sp,
                    modifier = Modifier.fillMaxWidth()
                        .padding(horizontal = 10.dp)
                        .padding(top = 10.dp)
                )
                HorizontalDivider(
                    modifier = Modifier.fillMaxWidth()
                        .padding(top = 10.dp)
                )
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    TextButton (
                        shape = RectangleShape,
                        onClick = {
                            onDismiss()
                        },
                        modifier = Modifier.weight(1.0f)) {
                        Text(
                            text = "取消",
                        )
                    }
                    VerticalDivider(
                        modifier = Modifier.height(30.dp)
                    )
                    TextButton (
                        shape = RectangleShape,
                        onClick = {
                            onConfirm()
                        },
                        modifier = Modifier.weight(1.0f)) {
                        Text(
                            text = "确认",
                        )
                    }
                }
            }
        }
    }
}


@Composable
fun SelectDialog(title: String, data: List<String>, selectedIndex: Int,
                 onSelect:(Int, String) -> Unit,
                 onDismiss: () -> Unit) {
    Dialog(
        onDismissRequest = {
            onDismiss()
        }
    ) {
        Card(
            modifier = Modifier.width(300.dp)
        ) {
            Column(
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = title,
                    fontSize = 17.sp,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier.padding(vertical = 10.dp)
                )
                LazyColumn(
                    modifier = Modifier.fillMaxWidth()
                        .height(300.dp)
                ) {
                    itemsIndexed(data) { index, text ->
                        Row(
                            modifier = Modifier.fillMaxWidth()
                                .height(40.dp)
                                .clickable {
                                    onSelect(index, text)
                                },
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            if (selectedIndex == index) {
                                Text(
                                    text = text,
                                    color = MaterialTheme.colorScheme.primary,
                                    fontSize = 14.sp,
                                    modifier = Modifier.weight(1.0f)
                                        .padding(start = 10.dp)
                                )
                                Icon(
                                    imageVector = Icons.Rounded.Check,
                                    contentDescription = text,
                                    modifier = Modifier.padding(end = 10.dp)
                                )
                            } else {
                                Text(
                                    text = text,
                                    fontSize = 14.sp,
                                    modifier = Modifier.fillMaxWidth(1.0f)
                                        .padding(horizontal = 10.dp)
                                )
                            }
                        }
                    }
                }
            }

        }
    }
}